package com.he.event.config;

import com.he.event.EventListener;
import com.he.event.util.SpringContextUtil;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ConditionalOnBean(EventListener.class)
public class EventConsumerConfiguration {

    @Value("${spring.application.name}")
    private String applicationName;

    @Autowired
    private SpringContextUtil springContextUtil;

    @Autowired
    private List<EventListener> eventListeners;

    @Bean
    public Queue getQueue() {
        return new Queue(applicationName + "-queue", true, false, false);
    }
    /**
     * 队列和交换机的绑定????????????
     * @param getQueue
     * @param getExchange
     * @return
     */
    @Bean
    public Binding getBinding(Queue getQueue, DirectExchange getExchange){

        //循环所有的EventListener实现类
        eventListeners.stream().forEach(event -> {
            //获得当前的处理器需要处理的事件类型 - 路由键
            String eventType = event.getEventType();
            System.out.println("绑定的路由键：" + eventType);
            Binding binding = BindingBuilder.bind(getQueue).to(getExchange).with(eventType);

            //动态将binding对象注册到Spring容器中
            springContextUtil.registerBean(eventType + event.hashCode(), binding);
        });

        return null;
    }
}
